package com.longrise.android.byjk.plugins.aboutme;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.common.MessageConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.PersonalInfoContract;
import com.longrise.android.byjk.plugins.aboutme.personalInfo.SecurityPresenter;

/**
 * Created by godliness on 2017/3/7.
 * 账号与安全
 */

public class SecurityActivity extends BaseActivity implements View.OnClickListener, PersonalInfoContract.SecurView {

    private static final String TAG = "SecurityActivity";

    private RelativeLayout mChangePassword;
    private RelativeLayout mPhone;
    private TextView mPhoneNum;
    private RelativeLayout mWechart;
    private TextView mWechartName;
    private RelativeLayout mRun;
    private TextView mRunPhone;

    private PersonalInfoContract.SecurPresenters mSecurPresenter;

    private WXBroadCast mWXBroadCast;
    private Toolbar mToolbar;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {

        }
        return R.layout.activity_security;
    }

    public void initPresenter() {
        mSecurPresenter = new SecurityPresenter(this, this);
        mSecurPresenter.init();
    }

    @Override
    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);

        mChangePassword = (RelativeLayout) findViewById(R.id.changepass_securact);
        mPhone = (RelativeLayout) findViewById(R.id.phone_securact);
        mPhoneNum = (TextView) findViewById(R.id.tvphone_securact);
        mWechart = (RelativeLayout) findViewById(R.id.wechart_securact);
        mWechartName = (TextView) findViewById(R.id.tv_wechart_securact);
        mRun = (RelativeLayout) findViewById(R.id.run_login);
        mRunPhone = (TextView) findViewById(R.id.tv_run_login);

        onEvent();

        initPresenter();
    }

    private void onEvent() {
        if (mChangePassword != null) {
            mChangePassword.setOnClickListener(this);
        }
        if (mPhone != null) {
            mPhone.setOnClickListener(this);
        }
        if (mWechart != null) {
            mWechart.setOnClickListener(this);
        }
        if (mRun != null) {
            mRun.setOnClickListener(this);
        }
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSecurPresenter.close();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.changepass_securact:
                mSecurPresenter.jumpPassword();
                break;

            case R.id.phone_securact:
                mSecurPresenter.jumpPhone();
                break;

            case R.id.wechart_securact:
                mSecurPresenter.wechatBundle();
                break;

            case R.id.run_login:
                mSecurPresenter.jumpRunLogin();
                break;

            default:
                break;
        }
    }

    @Override
    public void changePassword() {

    }

    @Override
    public void phoneBundle(String phone) {
        mPhoneNum.setText(phone);

    }

    @Override
    public void wechatBundle(String wechat) {
        mWechartName.setText(wechat);
    }

    @Override
    public void runLoginPhone(String phone, String openState) {
        if (!TextUtils.isEmpty(phone)) {
            mPhoneNum.setText(phone);
        }
        mRunPhone.setText(openState);
    }

    @Override
    public void colse() {
        finish();
    }

    @Override
    public void exit() {
        Intent intent = new Intent();
        intent.putExtra("key", true);
        setResult(100, intent);
        colse();
    }

    @Override
    public void setTitle(String title) {
        mToolbar.setTitle(title);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mSecurPresenter.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void startActivityForR(Intent intent, int requestCode) {
        this.startActivityForResult(intent, requestCode);
    }

    @Override
    public void startActivityForR(int requestCode, Class<?> cls) {
        Intent resultIntent = new Intent(this, cls);
        resultIntent.putExtra("type", requestCode);
        this.startActivityForResult(resultIntent, requestCode);
    }

    @Override
    public void toJump(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();
        registerBroadCast();
    }

    private void registerBroadCast() {
        if (mWXBroadCast == null) {
            mWXBroadCast = new WXBroadCast();
            IntentFilter filter = new IntentFilter();
            filter.addAction(MessageConstants.WX_BUNDLE_);
            filter.addAction(MessageConstants.VIP_UPDATE);
            registerReceiver(mWXBroadCast, filter);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWXBroadCast != null) {
            unregisterReceiver(mWXBroadCast);
        }
    }

    private class WXBroadCast extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent intent) {
            switch (intent.getAction()) {
                case MessageConstants.VIP_UPDATE:
//                    initVip();
                    break;

                case MessageConstants.WX_BUNDLE_:
                    if (!TextUtils.isEmpty(UserInfor.getInstance().getmWXName())) {
                        wechatBundle(UserInfor.getInstance().getmWXName());
                        mSecurPresenter.creataDilaog();
                    }
                    break;
            }
        }
    }
}
